<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        .wrapper {
            width: 800px;
            height: 600px;
            margin: 100px auto;
        }

        .wrapper .big-img {
            display: block;
            width: 800px;
            height: 400px;
        }

        .img-list {
            width: 100%;
            height: 200px;
        }

        .img-list .img-item {
            float: left;
            width: 200px;
            height: 200px;
            box-sizing: border-box;
        }

        .img-list .img-item.active {
            border: 2px solid red;
        }

        .img-list .img-item img {
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <img src="img/m1.jpg" alt="" class="big-img">
        <ul class="img-list">
            <li class="img-item active">
                <img src="img/m1.jpg" alt="">
            </li>
            <li class="img-item">
                <img src="img/m2.jpg" alt="">
            </li>
            <li class="img-item">
                <img src="img/m3.jpg" alt="">
            </li>
            <li class="img-item">
                <img src="img/m4.jpg" alt="">
            </li>
        </ul>
    </div>
    <script>
        var imgList = document.querySelectorAll('.img-list .img-item img');
        var targetImg = document.querySelector('.big-img');
        console.log(imgList);
        for (var i = 0; i < imgList.length; i++) {
            imgList[i].onclick = function () {
                // 1.移除之前的active
                console.log(imgList[i]);
                //排他 遍历所有的li 如果存在active 则移除

                for (var j = 0; j < imgList.length; j++) {
                    var item = imgList[j].parentNode;
                    if (item.classList.contains('active')) {
                        item.classList.remove('active');
                        break;
                    }

                }
                // 2.给自己添加active
                this.parentNode.classList.add('active');
                // 3.将图片传送给大图
                targetImg.src = this.src;
            }
        }
    </script>
</body>

</html>